﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using System.Threading;
using IToursDataService.Models;
using IToursDataService;
using IToursDataService.Helpers;

namespace ChinaDynastyTours.Views
{
    public partial class TourGroupScheduleForm : DevExpress.XtraEditors.XtraForm
    {
        public DataView _TourGroupDataTable { get; set; }
        //public int _Position { get; set; }
        //是否是查看
        //public bool _LookupState { get; set; }
        
        private DataView _ScheduleDataView = null;

        private string _Remark = "";
        //private string _EndTime = "00:00:00";


        public TourGroupScheduleForm(DataView tourGroupDataTable)//,int rowID)
        {
            InitializeComponent(); 

            _TourGroupDataTable = tourGroupDataTable;
            //_Position = rowID;
            //界面初始化时，备注窗口隐藏，没有值，将备注值存在_Remark中
           _Remark = _TourGroupDataTable[0]["Remark"].ToString();
        }


        #region 窗口初始化
        //窗口Load事件
        private void TourGroupScheduleForm_Load(object sender, EventArgs e)
        {
            
            #region 初始化国家下拉列表

            listBoxCountry.DataSource = WcfClient.UseService((IToursDataService.IBaseInfo b) => (b.GetCountryData())); //BaseInfoCtrl.GetCountryData();
            listBoxCountry.DisplayMember = "CountryName";
            listBoxCountry.ValueMember = "CountryCode";

            #endregion

            //#region 初始化客户下拉列表

            //this.customerGridControl.DataSource = BaseInfoCtrl.GetCustomerData();
            
            //#endregion
 
            //初始化团基本信息显示
            InitGui();

            //初始化计划内容
            LoadScheduleGridViewData();

            #region 团状态按钮处理

            DataRowView drv = (BindingContext[_TourGroupDataTable].Current as DataRowView);
            switch (WcfClient.UseService<ITeamGroupCtrl, TeamStateEnum>(t => t.GetTourGroupState(lblTourGroupID.Text)))//TeamGroupCtrl.GetTourGroupState(lblTourGroupID.Text))
            {
                case TeamStateEnum.计划:
                    break;
                case TeamStateEnum.成行:
                    tbTourgroupName.Enabled = false;
                    tbCountry.Enabled = false;
                    break;
                case TeamStateEnum.取消:
                    baseInfoPanel.Enabled = false;
                    ScheduleGridView.OptionsBehavior.Editable = false;
                    break;
                case TeamStateEnum.审核:
                case TeamStateEnum.终结:
                    baseInfoPanel.Enabled = false;
                    mbRemark.Enabled = false;
                    ScheduleGridView.OptionsBehavior.Editable = false;
                    break;
                default:
                    break;
            }

            #endregion
        }

        //计划行程数据加载
        private void LoadScheduleGridViewData()
        {
            #region 初始化计划行程‘城市’单元格控件内容
            //提供‘城市’选择         
            //StartAndEndCityEdit.DataSource = BaseInfoCtrl.GetCityData();

            #endregion

            _ScheduleDataView = new DataView(WcfClient.UseService<ITeamGroupCtrl, DataSet>(t => t.GetTourGroupSchedule(lblTourGroupID.Text)).Tables[0]);//TeamGroupCtrl.GetTourGroupSchedule(lblTourGroupID.Text).Tables[0]);
            scheduleGridControl.DataSource = _ScheduleDataView;

            //为行程计划网格‘视图’注册数据更新事件
            _ScheduleDataView.Table.RowDeleted += new DataRowChangeEventHandler(Table_RowDeleted);
            _ScheduleDataView.Table.RowChanged += new DataRowChangeEventHandler(Table_RowChanged);

            ScheduleGridView.FocusedRowHandle = ScheduleGridView.RowCount - 1;

        }

        //初始化界面内容
        private void InitGui()
        {

            //团状态信息
            lblTourGroupID.DataBindings.Add("Text", _TourGroupDataTable, "TourgroupID");
            lblTourGroupState.DataBindings.Add("Text", _TourGroupDataTable, "TourgroupState");


            tbCountry.DataBindings.Add("EditValue", _TourGroupDataTable, "Country");
            lblCountryCode.DataBindings.Add("Text", _TourGroupDataTable, "CountryCode");
            tbStartDate.DataBindings.Add("EditValue", _TourGroupDataTable, "StartDate");
            tbEndDate.DataBindings.Add("EditValue", _TourGroupDataTable, "EndDate");
            tbNightNum.DataBindings.Add("EditValue", _TourGroupDataTable, "StayOver");
            tbAdultNum.DataBindings.Add("EditValue", _TourGroupDataTable, "AdultNum");
            tbChildNum.DataBindings.Add("EditValue", _TourGroupDataTable, "ChildNum");
            tbTourgroupName.DataBindings.Add("EditValue", _TourGroupDataTable, "TourgroupName");
            cbGuesttype.DataBindings.Add("EditValue", _TourGroupDataTable, "Guesttype");
            cbServicetype.DataBindings.Add("EditValue", _TourGroupDataTable, "Servicetype");
            cbTourgrouptype.DataBindings.Add("EditValue", _TourGroupDataTable, "Tourgrouptype");
            tbAccompanyNum.DataBindings.Add("EditValue", _TourGroupDataTable, "AccompanyNum");
            //tbCustomerCode.DataBindings.Add("EditValue", _TourGroupDataTable, "CustomerCode");
            //lblCustomerName.DataBindings.Add("Text", _TourGroupDataTable, "CustomerName");
            cbOwnerA.DataBindings.Add("EditValue", _TourGroupDataTable, "OwnerA");
            cbOwnerB.DataBindings.Add("EditValue", _TourGroupDataTable, "OwnerB");
            ckHasGuide.DataBindings.Add("Checked", _TourGroupDataTable, "HasGuide");
            tbBuildGroupDate.DataBindings.Add("EditValue", _TourGroupDataTable, "BuildGroupDate");
            mbRemark.DataBindings.Add("EditValue", _TourGroupDataTable, "Remark");
        } 
        #endregion

        #region 计划行程数据变化后的后台处理
        void Table_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change || e.Action == DataRowAction.ChangeCurrentAndOriginal || e.Action == DataRowAction.ChangeOriginal)
            {

                TourGroupScheduleEntity schedule = new TourGroupScheduleEntity();
                schedule.TourgroupID = this.lblTourGroupID.Text;
                schedule.Date = DateTime.Parse(e.Row["Date"].ToString()).ToString("yyyy-MM-dd");
                schedule.Time = DateTime.Parse(e.Row["Time"].ToString()).ToString("HH:mm:ss");
                schedule.StayOver = int.Parse(e.Row["StayOver"].ToString());
                schedule.Journeytype = e.Row["Journeytype"].ToString();
                schedule.StartCity = e.Row["StartCity"].ToString();
                schedule.DestinationCity = e.Row["DestinationCity"].ToString();
                schedule.TrafficType = e.Row["TrafficType"].ToString();
                schedule.Traffic = e.Row["Traffic"].ToString();
                schedule.TravelAgencyID = int.Parse(e.Row["TravelAgencyID"].ToString());
                schedule.TravelAgencyName = e.Row["TravelAgencyName"].ToString();
                schedule.Reception = e.Row["Reception"].ToString();
                schedule.TicketType = e.Row["TicketType"].ToString();
                schedule.Context = e.Row["Context"].ToString();
                schedule.Dining = e.Row["Dining"].ToString();
                schedule.Hotel = e.Row["Hotel"].ToString();
                schedule.HotelID = int.Parse(e.Row["HotelID"].ToString());
                schedule.Seat = e.Row["Seat"].ToString();
                schedule.ScheduleMethod = e.Row["ScheduleMethod"].ToString();

                schedule.TourGroupScheduleID = int.Parse(e.Row["TourGroupScheduleID"].ToString());

                WcfClient.UseService<ITeamGroupCtrl, int>(t => t.UpdateTourGroupSchedule(schedule)); //TeamGroupCtrl.UpdateTourGroupSchedule(schedule);
            }
        }

        private void Table_RowDeleted(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Delete)
            {
                string tourGroupScheduleID = e.Row["TourGroupScheduleID", DataRowVersion.Original].ToString();
                if (tourGroupScheduleID == "")
                    return;

                int scheduleID = int.Parse(tourGroupScheduleID);
                //删除行程计划数据
                WcfClient.UseService<ITeamGroupCtrl, int>(t => t.DeleteTourGroupSchedule(scheduleID)); //TeamGroupCtrl.DeleteTourGroupSchedule(scheduleID);
                //删除行程计划关联的宾馆、地接社、交通（火车、飞机、汽车）预定信息及费用主表
                WcfClient.UseService<IHotelOrderCtrl>(h => h.DeleteHotelOrder(scheduleID)); //HotelOrderCtrl.DeleteHotelOrder(scheduleID);
                WcfClient.UseService<ITravelOrderCtrl>(t => t.DeleteTravelOrder(scheduleID)); //TravelOrderCtrl.DeleteTravelOrder(scheduleID);
                
                //火车飞机预订由‘计调’来完成删除
                //TrainOrderCtrl.DeleteTrainOrder(scheduleID);
                //FlightOrderCtrl.DeleteFlightOrder(scheduleID);
            }
        }

        #endregion

        #region  火车、飞机生成新的或修改已有的预订及费用数据

        /// <summary>
        /// 火车生成新的或修改已有的预订及费用信息
        /// </summary>
        /// <param name="tourGroupScheduleId"></param>
        /// <param name="e"></param>
        private void SaveTrainOrderData(int tourGroupScheduleId, DataRow dr)
        {
            //清除已有火车或飞机预订信息
            //TrainOrderCtrl.DeleteTrainOrder(tourGroupScheduleId);          //同时删除存在此行程计划的火车预订，两种预订不能同时存在
            //FlightOrderCtrl.DeleteFlightOrder(tourGroupScheduleId);        //同时删除存在此行程计划的飞机预订，两种预订不能同时存在

            //DataTable dtTrainOrderData = TrainOrderCtrl.GetTrainOrderData(tourGroupScheduleId);
            //int expenseID = -1;

            //if (dtTrainOrderData != null && dtTrainOrderData.Rows.Count > 0)
            //    expenseID = int.Parse(dtTrainOrderData.Rows[0]["ExpenseID"].ToString());//取得费用标识

            string journeyType = dr["Journeytype"].ToString();//ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, "Journeytype").ToString();
            string time = DateTime.Parse(dr["Time"].ToString()).ToString("HH:mm:ss");
            string date = DateTime.Parse(dr["Date"].ToString()).ToString("yyyy-MM-dd");

            //创建火车预定信息内容
            TrainOrderEntity trainOrder = new TrainOrderEntity();
            trainOrder.OrderState = OrderStateEnum.未订.ToString();/*重点内容，初始化订单状态为‘未定’*/
            trainOrder.TrainCode = dr["Traffic"].ToString();
            trainOrder.BeginPosition = dr["StartCity"].ToString();
            trainOrder.EndPosition = dr["DestinationCity"].ToString();
            switch (EnumHelper.Journey_Str2Enum(journeyType, true))
            {
                case JourneyTypeEnum.入境:
                case JourneyTypeEnum.抵:
                    trainOrder.StartTime = "00:00:00";
                    trainOrder.EndTime = time;
                    break;
                case JourneyTypeEnum.离境:
                case JourneyTypeEnum.离:
                    trainOrder.StartTime = time;
                    trainOrder.EndTime = "00:00:00";
                    break;
                default:
                    break;
            }
            trainOrder.StartDate = date;
            trainOrder.EndDate = date;
            trainOrder.HardSeatQuantity = 0;
            trainOrder.SoftSeatQuantity = 0;
            trainOrder.HardSleeperBottomQuantity = 0;
            trainOrder.HardSleeperMiddleQuantity = 0;
            trainOrder.HardSleeperTopQuantity = 0;
            trainOrder.HighBoxQuantity=0;
            trainOrder.ChildSeatQuantity = 0;
            trainOrder.Remark = "";
            
            trainOrder.SoftSleeperBottomPrice=0;
            trainOrder.SoftSleeperBottomQuantity=0;
            trainOrder.SoftSleeperTopPrice=0;
            trainOrder.SoftSleeperTopQuantity=0;
            trainOrder.HandlingCharge = 0;
            trainOrder.HardSeatPrice = 0;
            trainOrder.ChildSeatPrice=0;
            trainOrder.HardSleeperTopPrice = 0;
            trainOrder.HardSleeperMiddlePrice = 0;
            trainOrder.SoftSeatPrice = 0;
            trainOrder.HardSleeperBottomPrice = 0;
            trainOrder.HighBoxPrice = 0;

            trainOrder.AccompanyNum = int.Parse(tbAccompanyNum.EditValue.ToString());
            trainOrder.AdultNum = int.Parse(tbAdultNum.EditValue.ToString());
            trainOrder.ChildNum = int.Parse(tbChildNum.EditValue.ToString());
            trainOrder.TourgroupID = lblTourGroupID.Text;
            trainOrder.TourgroupName=tbTourgroupName.Text;
            trainOrder.TourGroupScheduleID=tourGroupScheduleId;
            trainOrder.ProviderID = -1;
            trainOrder.ProviderName = "";

            trainOrder.Seat = dr["Seat"].ToString();

            //创建费用
            ExpenseEntity expense = new ExpenseEntity()
            {
                //ExpenseID = _ExpenseID,
                TourgroupID = lblTourGroupID.Text,
                TourgroupName = WcfClient.UseService<ITeamGroupCtrl, string>(t => t.GetTeamGroupNameById(lblTourGroupID.Text)),//TeamGroupCtrl.GetTeamGroupNameById(lblTourGroupID.Text),
                ProviderID = -1,
                ProviderName = "",
                ServiceName = dr["StartCity"].ToString() + "-" + dr["DestinationCity"].ToString() + " 火车票",
                Budget = 0,
                PayTotal = 0,
                ApplyPayTotal = 0,
                AdjustAmount = 0,
                Status = ExpenseStatusEnum.计划.ToString(),
                TypeBelong = ProviderTypeEnum.火车票.ToString(),
                ApplyPay = 0,
                PayStatus = PayApplyStatusEnum.未付.ToString(),
                CreateByUserName = Thread.CurrentPrincipal.Identity.Name, //UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name)
                TourGroupScheduleID = tourGroupScheduleId,
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd"),
                ExpenseTypeID = 4,//所属费用类型(费用类型表中存储着编号，此编号是固定的，1房费、2团费、3餐费、4交通费、5门票、6杂费)
                ExpenseType = WcfClient.UseService((IToursDataService.IBaseInfo b) => (b.GetExpenseTypeNameByID(4)))//BaseInfoCtrl.GetExpenseTypeNameByID(4)//同上
           };
            WcfClient.UseService<ITrainOrderCtrl,int>(t => t.CreateTrainOrderAndExpenseData(expense, trainOrder)); //TrainOrderCtrl.CreateTrainOrderAndExpenseData(expense, trainOrder);
        } 
        
        /// <summary>
        /// 飞机生成新的或修改已有的预订及费用信息
        /// </summary>
        /// <param name="tourGroupScheduleId"></param>
        /// <param name="e"></param>
        //private void SaveFlightOrderData(int tourGroupScheduleId, DataRow dr)
        //{

        //    string journeyType = dr["Journeytype"].ToString();//ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, "Journeytype").ToString();
        //    string time = DateTime.Parse(dr["Time"].ToString()).ToString("HH:mm:ss");
        //    string date = DateTime.Parse(dr["Date"].ToString()).ToString("yyyy-MM-dd");

        //    //创建飞机预定信息内容
        //    FlightOrderEntity flightOrder = new FlightOrderEntity();
        //    flightOrder.OrderState = OrderStateEnum.未订.ToString();/*重点内容，初始化订单状态为‘未定’*/
        //    flightOrder.FlightCode = dr["Traffic"].ToString();
        //    flightOrder.AccompanyNum = int.Parse(tbAccompanyNum.EditValue.ToString());
        //    flightOrder.AdultNum = int.Parse(tbAdultNum.EditValue.ToString());
        //    flightOrder.BabyNum = 0;//int.Parse(tbBabyNum.EditValue.ToString())
        //    flightOrder.ChildNum = int.Parse(tbChildNum.EditValue.ToString());
        //    //flightOrder.AdultQuantity=0;
        //    //flightOrder.ChildQuantity=0;
        //    //flightOrder.BabyQuantity=0;
        //    //flightOrder.AccompanyQuantity=0;
        //    flightOrder.BeginPosition = dr["StartCity"].ToString(); //ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, "StartCity").ToString();
        //    flightOrder.EndPosition = dr["DestinationCity"].ToString(); //ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, "DestinationCity").ToString();
        //    flightOrder.StartDate = date;
        //    flightOrder.EndDate = date;

        //    switch (EnumHelper.Journey_Str2Enum(journeyType, true))
        //    {
        //        case JourneyTypeEnum.入境:
        //        case JourneyTypeEnum.抵:
        //            flightOrder.StartTime = "00:00:00";
        //            flightOrder.EndTime = time;
        //            break;
        //        case JourneyTypeEnum.离境:
        //        case JourneyTypeEnum.离:
        //            flightOrder.StartTime = time;
        //            flightOrder.EndTime = "00:00:00";
        //            break;
        //        default:
        //            break;
        //    }
        //    flightOrder.Remark = "";
        //    flightOrder.AccompanyUnitPrice = 0;
        //    flightOrder.AdultUnitPrice = 0;
        //    flightOrder.BabyUnitPrice = 0;
        //    flightOrder.ChildUnitPrice = 0;
        //    flightOrder.HandlingCharge = 0;
        //    //FlightID=0;
        //    flightOrder.TourGroupScheduleID = tourGroupScheduleId;
        //    flightOrder.TourgroupID = lblTourGroupID.Text;
        //    flightOrder.TourgroupName = tbTourgroupName.Text;
        //    flightOrder.ProviderID = -1;
        //    flightOrder.ProviderName = "";

        //    flightOrder.Seat = dr["Seat"].ToString();
            
        //    //创建费用
        //    ExpenseEntity expense = new ExpenseEntity()
        //    {
        //        //ExpenseID = _ExpenseID,
        //        TourgroupID = lblTourGroupID.Text,
        //        TourgroupName=TeamGroupCtrl.GetTeamGroupNameById(lblTourGroupID.Text),
        //        ProviderID = -1,
        //        ProviderName = "",
        //        ServiceName = dr["StartCity"].ToString()+"-"+dr["DestinationCity"].ToString()+" 飞机票",
        //        Budget = 0,
        //        PayTotal = 0,
        //        ApplyPayTotal = 0,
        //        AdjustAmount = 0,
        //        Status = ExpenseStatusEnum.计划.ToString(),
        //        TypeBelong = ProviderTypeEnum.飞机票.ToString(),
        //        ApplyPay=0,
        //        PayStatus= PayApplyStatusEnum.未付.ToString(),
        //        CreateByUserName = Thread.CurrentPrincipal.Identity.Name,//UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name)
        //        TourGroupScheduleID = tourGroupScheduleId,
        //        CreateTime = DateTime.Now.ToString("yyyy-MM-dd"),
        //        ExpenseTypeID = 4,//所属费用类型(费用类型表中存储着编号，此编号是固定的，1房费、2团费、3餐费、4交通费、5门票、6杂费)
        //        ExpenseType = WcfClient.UseService((IToursDataService.IBaseInfo b) => (b.GetExpenseTypeNameByID(4)))//BaseInfoCtrl.GetExpenseTypeNameByID(4)//同上
        //    };
        //    WcfClient.UseService<IFlightOrderCtrl>(f=>f.CreateFlightOrderAndExpenseData(expense, flightOrder));//FlightOrderCtrl.CreateFlightOrderAndExpenseData(expense, flightOrder);
            
        //}  
        
        #endregion

        #region 窗口底部系列按钮事件处理


        //打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            new SchedulePrintForm(lblTourGroupID.Text).ShowDialog(this);
        }
        //备注
        private void btnRemark_Click(object sender, EventArgs e)
        {
            panelRemark.Visible = true;
        }
        private void btnRemarkClose_Click(object sender, EventArgs e)
        {
            panelRemark.Visible = false;
        }
        /// <summary>
        /// 变更通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChangeNotify_Click(object sender, EventArgs e)
        {
            ChangeNotifyForm changeNofityForm = new ChangeNotifyForm(lblTourGroupID.Text);
            changeNofityForm.ShowDialog(this);
        }
        /// <summary>
        /// 客人列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGuestList_Click(object sender, EventArgs e)
        {
            GuestListForm guestListForm = new GuestListForm(lblTourGroupID.Text);
            guestListForm.ShowDialog(this);
        }

        private void btnLookGuide_Click(object sender, EventArgs e)
        {
            new GuideStatusLookForm(lblTourGroupID.Text).ShowDialog(this);
        }
        private void btnRestaurant_Click(object sender, EventArgs e)
        {
            new RestaurantOrderStatusLookForm(lblTourGroupID.Text).ShowDialog(this);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion

        #region 行程计划GridView事件处理

        private void StartAndEndCityEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ////暂时屏蔽，用LookUpEdit提供的城市的选择方式
            //DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
            //CitySelectedToolForm city = new CitySelectedToolForm();

            //if (city.ShowDialog(this) == DialogResult.OK)
            //    btnEdit.EditValue = city.ReturnCityName;
        }

        private void TrafficEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            //string TrafficType = ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, ScheduleGridView.Columns["TrafficType"]).ToString();
            //string tourGroupScheduleID = ScheduleGridView.GetDataRow(ScheduleGridView.FocusedRowHandle)["TourGroupScheduleID"].ToString();

            //if (string.IsNullOrEmpty(TrafficType))
            //{
                
            //    MessageBox.Show("您还没有选择交通工具类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}

            //DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
            //switch (EnumHelper.Traffic_Str2Enum(TrafficType, true))//CommonEnum.GetTrafficType(TrafficType))
            //{
            //    case TrafficTypeEnum.飞机:
            //        FlightOrderForm flightOrderForm = new FlightOrderForm()
            //        {
            //            _StartCity=ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, ScheduleGridView.Columns["StartCity"]).ToString(),
            //            _EndCity=ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, ScheduleGridView.Columns["DestinationCity"]).ToString(),
            //        };
            //        if (flightOrderForm.ShowDialog(this) == DialogResult.OK)//设置各项返回值到表格中
            //        {
            //            btnEdit.EditValue = flightOrderForm._FlightCode;
            //            //ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "StartCity", flightOrderForm._StartCity);
            //            //ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "DestinationCity", flightOrderForm._EndCity);
            //            string journeyType = ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, "Journeytype").ToString();
            //            switch (EnumHelper.Journey_Str2Enum(journeyType, true))
            //            {
            //                case JourneyTypeEnum.入境:
            //                case JourneyTypeEnum.抵:
            //                    ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "Time", flightOrderForm._EndTime);
            //                    break;
            //                case JourneyTypeEnum.离境:
            //                case JourneyTypeEnum.离:
            //                    ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "Time", flightOrderForm._StartTime);
            //                    _EndTime = flightOrderForm._EndTime;
            //                    break;
            //                default:
            //                    break;
            //            }
            //        }
            //        break;
            //    case TrafficTypeEnum.火车:
            //        TrainOrderForm trainOrderForm = new TrainOrderForm()
            //        {
            //            _StartCity = ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, ScheduleGridView.Columns["StartCity"]).ToString(),
            //            _EndCity = ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, ScheduleGridView.Columns["DestinationCity"]).ToString(),
            //        };
            //        if (trainOrderForm.ShowDialog(this) == DialogResult.OK)//设置各项返回值到表格中
            //        {
            //            btnEdit.EditValue = trainOrderForm._TrainCode;
            //            //ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "StartCity", trainOrderForm._StartCity);
            //            //ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "DestinationCity", trainOrderForm._EndCity);
            //            string journeyType = ScheduleGridView.GetRowCellValue(ScheduleGridView.FocusedRowHandle, "Journeytype").ToString();
            //            switch (EnumHelper.Journey_Str2Enum(journeyType, true))
            //            {
            //                case JourneyTypeEnum.入境:
            //                case JourneyTypeEnum.抵:
            //                    ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "Time", trainOrderForm._EndTime);
            //                    break;
            //                case JourneyTypeEnum.离境:
            //                case JourneyTypeEnum.离:
            //                    ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "Time", trainOrderForm._StartTime);
            //                    _EndTime = trainOrderForm._EndTime;
            //                    break;
            //                default:
            //                    break;
            //            }
            //        }
            //        break;
            //    case TrafficTypeEnum.汽车:
            //        MessageBox.Show("请自行填写‘汽车’服务信息！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        break;
            //    default:
            //        MessageBox.Show("您选择的交通工具类型目前不被支持！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        break;
            //}
            
        }

        private void TravelAgencyNameEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            //string tourGroupScheduleID = ScheduleGridView.GetDataRow(ScheduleGridView.FocusedRowHandle)["TourGroupScheduleID"].ToString();
            //string destinationCity = ScheduleGridView.GetDataRow(ScheduleGridView.FocusedRowHandle)["DestinationCity"].ToString();

            //if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Delete)
            //{
            //    if (MessageBox.Show("确认删除供应商吗？", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            //    {
            //        if (TeamGroupCtrl.CheckExistPayApply(tourGroupScheduleID, ProviderTypeEnum.旅行社.ToString()))
            //        {
            //            MessageBox.Show("已经申请付款，不能删除", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //            tbTourgroupName.Focus();
            //            return;
            //        }
                    
            //        DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
            //        btnEdit.Tag = -1;
            //        btnEdit.EditValue = "";
            //        ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "TravelAgencyID", -1);
            //        ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "Reception", "");
            //        TravelOrderCtrl.DeleteTravelOrder(int.Parse(tourGroupScheduleID));
            //    }
            //}
            //else
            //{
            //    TravelOrderForm travelForm = new TravelOrderForm(destinationCity)
            //    {
            //        _TourGroupID = lblTourGroupID.Text,
            //        _TourGroupState = lblTourGroupState.Text,
            //        _TourGroupScheduleID = int.Parse(tourGroupScheduleID),
            //        _AdultNum = int.Parse(tbAdultNum.Text)
            //    };
            //    if (travelForm.ShowDialog(this) == DialogResult.OK)
            //    {
            //        DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
            //        btnEdit.EditValue = travelForm.ReturnTravelAgencyName;
            //        ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "TravelAgencyID", travelForm.ReturnTravelAgencyID);
            //        ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "Reception", travelForm.ReturnContact);
            //    }
            //}
        }

        private void HotelEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            //string tourGroupScheduleID = ScheduleGridView.GetDataRow(ScheduleGridView.FocusedRowHandle)["TourGroupScheduleID"].ToString();
            //string destinationCity = ScheduleGridView.GetDataRow(ScheduleGridView.FocusedRowHandle)["DestinationCity"].ToString();
            //DateTime startDate = DateTime.Parse(ScheduleGridView.GetDataRow(ScheduleGridView.FocusedRowHandle)["Date"].ToString());
            //string stayOver = ScheduleGridView.GetDataRow(ScheduleGridView.FocusedRowHandle)["StayOver"].ToString();

            //if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Delete)
            //{
            //    if (MessageBox.Show("确认删除供应商吗？", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            //    {
            //        if (TeamGroupCtrl.CheckExistPayApply(tourGroupScheduleID, ProviderTypeEnum.宾馆.ToString()))
            //        {
            //            MessageBox.Show("已经申请付款，不能删除", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //            tbTourgroupName.Focus();
            //            return;
            //        }

                    
            //        DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
            //        btnEdit.Tag = -1;
            //        btnEdit.EditValue = "";
            //        ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "HotelID", -1);
            //        ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "ScheduleMethod", "");
            //        HotelOrderCtrl.DeleteHotelOrder(int.Parse(tourGroupScheduleID));

            //    }
            //}
            //else
            //{
            //    HotelOrderForm hotelForm = new HotelOrderForm(destinationCity)
            //    {
            //        _TourGroupID = lblTourGroupID.Text,
            //        _TourGroupState = lblTourGroupState.Text,
            //        _TourGroupScheduleID = int.Parse(tourGroupScheduleID),
            //        _ArrivalTime = startDate,
            //        _StayOver = int.Parse(stayOver)
            //    };
            //    if (hotelForm.ShowDialog(this) == DialogResult.OK)
            //    {
            //        DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
            //        ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "HotelID", hotelForm._ReturnHotelID);
            //        ScheduleGridView.SetRowCellValue(ScheduleGridView.FocusedRowHandle, "ScheduleMethod", hotelForm._ReturnScheduleMethod);
            //        btnEdit.EditValue = hotelForm._ReturnHotelName;
            //    }
            //}
        }



        #endregion

        #region 行程计划窗口关闭
        
        private void TourGroupScheduleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //激发行程计划的GrideView数据更新
            if (ScheduleGridView.FocusedRowModified)
                ScheduleGridView.FocusedRowHandle = ScheduleGridView.FocusedRowHandle == 0 ? 1 : 0;

            //激发团数据更新,转移一下焦点即可
            baseInfoPanel.Focus();
            //if (tbAccompanyNum.Focused)
            //    tbAdultNum.Focus();
            //else
            //    tbAccompanyNum.Focus();
        }

        #endregion

        #region 行数据校验

        private void ScheduleGridView_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            DataRowView dr = e.Row as DataRowView;
            if (dr["Journeytype"].ToString() == "")
                return;

            if (JourneyTypeEnum.离 == EnumHelper.Journey_Str2Enum(dr["Journeytype"].ToString(), true) || JourneyTypeEnum.离境 == EnumHelper.Journey_Str2Enum(dr["Journeytype"].ToString(), true))
            {
                if (dr["Traffic"].ToString() == "")
                {
                    ScheduleGridView.SetColumnError(ScheduleGridView.Columns["Traffic"], "车次信息不能为空", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default);
                    e.Valid = false;
                }
                if (dr["TrafficType"].ToString() == "")
                {
                    ScheduleGridView.SetColumnError(ScheduleGridView.Columns["TrafficType"], "交通工具不能为空", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default);
                    e.Valid = false;
                }
                if (dr["TicketType"].ToString() == "")
                {
                    ScheduleGridView.SetColumnError(ScheduleGridView.Columns["TicketType"], "订票信息不能为空", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default);
                    e.Valid = false;
                }
                if (dr["Seat"].ToString() == "")
                {
                    ScheduleGridView.SetColumnError(ScheduleGridView.Columns["Seat"], "席位信息不能为空", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default);
                    e.Valid = false;
                }
            }
        }

        private void ScheduleGridView_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                DataRowView dr = e.Row as DataRowView;

                if (dr["Traffic"].ToString() == "" || dr["TrafficType"].ToString() == "" || dr["TicketType"].ToString() == "" || dr["Seat"].ToString() == "")
                //{
                    //MessageBox.Show("您所填写的行内容不完整\n　请填写完整", "错误", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
                //}
                //else
                //    e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.Ignore;
            }
        }
        #endregion

        #region 备注框可移动
        Point mouse_offset;

        private void panelRemark_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);
        }

        private void panelRemark_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);
                ((Control)sender).Location = ((Control)sender).Parent.PointToClient(mousePos);
            }
        }
        #endregion



    }
}
